class AccederController < ApplicationController

  def index
    if current_user
      @people = User.all
      @people.delete(current_user)
    else

    end
  end

  def error_explanation
  end

  def create
    # find user:
    auth = request.env["omniauth.auth"]
    if params[:provider] == "twitter"
      if tp = TwitterProfile.find_by_uid( auth["uid"] )
        tp.update_with_omniauth( auth )
        @user = tp.user
      else
        @user = User.create(:default_profile => "twitter", :token => random_token)
        @user.twitter_profiles.create_with_omniauth( auth )
      end
    else
      raise "You can only authenticate with your twitter account right now."
    end
    if @user
      generate_token
      cookies.permanent[:user_id] = @user.token
      redirect_to :action => 'index'
    else
      redirect_to :action => 'index'
    end
  end

  def logout
    cookies.delete :user_id
    redirect_to :action => 'index'
  end

  def routing_error
    render_not_found(nil)
  end

  private
    def generate_token
      @user.token = random_token
      while @user.invalid?
        @user.token = random_token
      end
      @user.save
    end
    def random_token
      SecureRandom.hex(40)
    end

end
